package com.jz.digital.generate_get

import android.app.Activity
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Color
import android.text.InputType
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import com.jz.digital.R
import com.jz.digital.base.BaseClickActivity
import com.jz.digital.bean.ServerBean
import com.jz.digital.scan.ScanCodeActivity
import com.jz.digital.utils.DataUtil
import com.jz.digital.utils.systembar.ImmersionBar
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_chengpin_guige_xinghao
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_gongdan_id
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_gongdan_zuqiu_shuliang
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_gongying_shanghao
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_gongying_shangming
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_guige_xinhao
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_kefang_pinming
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_kehu_mingcheng
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_lingyong_shuliang
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_yilingyong_shuliang
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_yuan_kucun_shu
import kotlinx.android.synthetic.main.activity_material_requisition_layout.label_yuanliao_id
import kotlinx.android.synthetic.main.activity_material_requisition_layout.sv_parent
import kotlinx.android.synthetic.main.activity_material_requisition_layout.tv_commit
import kotlinx.android.synthetic.main.activity_material_requisition_pre_layout.label_scan
import kotlinx.android.synthetic.main.activity_material_requisition_pre_layout.label_yuan_liao
import kotlinx.android.synthetic.main.layout_common_title.layout_parent

/**
 * @author jiangdz
 * @date 2024/11/19
 * @time 19:26
 * 用途:生产领料
 */
class MaterialRequisitionCommitActivity : BaseClickActivity<MaterialRequisitionCommitPresenter>(),
    IMaterialRequisitionCommitView {
    /**
     * 工单输入框焦点
     */
    private var mOrderEditFocus = false
    override fun clickAction(view: View) {
        when (view.id) {
            //提交
            R.id.tv_commit -> {
                if (label_lingyong_shuliang?.getEditText().isNullOrEmpty()) {
                    showToast(R.string.input_lingyong_num)
                    return
                }
                if (DataUtil.toInt(label_lingyong_shuliang?.getEditText()) < 0) {
                    showToast(R.string.the_lingyong_cannot_be_negative)
                    return
                }
                if (DataUtil.toInt(label_lingyong_shuliang?.getEditText()) > DataUtil.toInt(
                        label_yuan_kucun_shu?.getContentText()
                    )
                ) {
                    showToast(R.string.irregularity_tips)
                    return
                }
                commit(true)
            }
        }
    }


    override fun getLayoutResId(): Int {
        return R.layout.activity_material_requisition_layout
    }

    override fun initData() {
        super.initData()
        mPresenter.initData()
    }


    override fun initViews() {
        super.initViews()
        ImmersionBar.with(this).statusBarColor(R.color.main_color)
            .titleBarMarginTop(layout_parent)
            .init()
        setTitle(R.string.sheng_chang_ling_liao)
        mTitleBar.getTitleLayoutAll(this).setBackgroundColor(getColor(R.color.main_color))
        mTitleBar.setLeftImgTint(ColorStateList.valueOf(Color.WHITE))
        mTitleBar.setTitleColor(Color.WHITE)
        label_lingyong_shuliang?.getEditWidget()?.inputType = InputType.TYPE_CLASS_NUMBER
        label_scan?.getRightImage()?.setOnClickListener {
            clearFocus()
            clearBackContent()
            startActivityForResult(
                Intent(this@MaterialRequisitionCommitActivity, ScanCodeActivity::class.java),
                scanGongDanRequestCode
            )
        }
        label_yuan_liao?.getRightImage()?.setOnClickListener {
            clearFocus()
            clearBackContent()
            startActivityForResult(
                Intent(this@MaterialRequisitionCommitActivity, ScanCodeActivity::class.java),
                scanYuanLiaoRequestCode
            )
        }
        label_scan?.getContentWidget()?.gravity = Gravity.END
        label_yuan_liao?.getContentWidget()?.gravity = Gravity.END
        tv_commit?.setOnClickListener(this)
        label_scan?.getEditWidget()?.onFocusChangeListener =
            View.OnFocusChangeListener { p0, p1 ->
                mOrderEditFocus = p1
                if (!p1) {
                    commit(false)
                } else {
                    clearWorkOrderAssignmentData()
                }
            }
        label_yuan_liao?.getEditWidget()?.onFocusChangeListener =
            View.OnFocusChangeListener { p0, p1 ->
                mOrderEditFocus = p1
                if (!p1) {
                    commit(false)
                } else {
                    label_yuan_liao?.getEditWidget()?.setText("")
                    clearBackContent()
                }
            }
        // 处理 ScrollView 的触摸事件，使 EditText 失去焦点
        sv_parent.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_MOVE) {
                // 隐藏软键盘并清除 EditText 焦点
                if (mOrderEditFocus) {
                    label_scan?.getEditWidget()?.clearFocus()
                    label_yuan_liao?.getEditWidget()?.clearFocus()
                }
            }
            false
        }
        label_lingyong_shuliang?.getEditWidget()?.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                label_lingyong_shuliang?.getEditWidget()?.setText("")
            }
        }
    }

    /**
     * 清空工单指定数据
     */
    private fun clearWorkOrderAssignmentData() {
        label_scan?.getEditWidget()?.setText("")
        label_yuanliao_id?.getContentWidget()?.text = ""
        label_gongdan_id?.getContentWidget()?.text = ""
        label_kehu_mingcheng?.getContentWidget()?.text = ""
        label_kefang_pinming?.getContentWidget()?.text = ""
        label_chengpin_guige_xinghao?.getContentWidget()?.text = ""
    }

    /**
     * 提交
     */
    private fun commit(exit: Boolean) {
        if (label_yuan_liao?.getEditText().isNullOrEmpty()) {
            showToast(getString(R.string.yuan_liao_no_null))
            return
        }
        mPresenter.commit(
            barcode = label_yuan_liao?.getEditText() ?: "",
            no1 = label_yuanliao_id?.getContentText() ?: "",
            no2 = label_scan?.getEditText() ?: "",
            no3 = label_gongying_shanghao?.getContentText() ?: "",
            no4 = label_gongying_shangming?.getContentText() ?: "",
            no5 = label_guige_xinhao?.getContentText() ?: "",
            no6 = label_gongdan_id?.getContentText() ?: "",
            no7 = label_kehu_mingcheng?.getContentText() ?: "",
            no8 = label_kefang_pinming?.getContentText() ?: "",
            no9 = label_chengpin_guige_xinghao?.getContentText() ?: "",
            qty1 = DataUtil.toInt(label_yuan_kucun_shu?.getContentText()),
            qty2 = DataUtil.toInt(label_lingyong_shuliang?.getEditText()),
            qty3 = DataUtil.toInt(
                label_gongdan_zuqiu_shuliang?.getContentText()
            ),
            qty4 = DataUtil.toInt(label_yilingyong_shuliang?.getContentText()),
            exit = exit
        )
    }

    companion object {
        const val scanYuanLiaoRequestCode = 100
        const val scanGongDanRequestCode = 101

        @JvmStatic
        fun startActivity(activity: Activity) {
            Intent(activity, MaterialRequisitionCommitActivity::class.java)
                .apply {
                    activity.startActivity(this)
                }
        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == scanGongDanRequestCode) {
            val scanCode = data?.getStringExtra(ScanCodeActivity.KEY_DATA) ?: ""
            if (scanCode.isEmpty()) return
            label_scan?.getEditWidget()?.setText(scanCode)
            commit(false)
        } else if (requestCode == scanYuanLiaoRequestCode) {
            val scanCode = data?.getStringExtra(ScanCodeActivity.KEY_DATA) ?: ""
            if (scanCode.isEmpty()) return
            label_yuan_liao?.getEditWidget()?.setText(scanCode)
            commit(false)
        }
    }

    override fun showLoading() {
        showLoadingDialog()
    }

    override fun dismissLoading() {
        dismissLoadingDialog(false)
    }

    override fun commitSuccess(data: ServerBean.ShengChangLinLiaoResponse, exit: Boolean) {
        if (exit) {
            showToast(R.string.commit_success)
            clearShowContent()
            return
        }

        label_yuanliao_id?.getContentWidget()?.text = data.no1
        label_scan?.getEditWidget()?.setText(data.no2)
        label_gongying_shanghao?.getContentWidget()?.text = data.no3
        label_gongying_shangming?.getContentWidget()?.text = data.no4
        label_guige_xinhao?.getContentWidget()?.text = data.no5
        label_gongdan_id?.getContentWidget()?.text = data.no6
        label_kehu_mingcheng?.getContentWidget()?.text = data.no7
        label_kefang_pinming?.getContentWidget()?.text = data.no8
        label_chengpin_guige_xinghao?.getContentWidget()?.text = data.no9
        label_yuan_kucun_shu?.getContentWidget()?.text = data.qty1
        label_lingyong_shuliang?.getEditWidget()?.setText(data.qty2)
        label_gongdan_zuqiu_shuliang?.getContentWidget()?.text = data.qty3
        label_yilingyong_shuliang?.getContentWidget()?.text = data.qty4
    }

    /**
     * 清除显示内容
     */
    private fun clearShowContent() {
        clearFocus()
        label_scan?.getEditWidget()?.setText("")
        label_yuan_liao?.getEditWidget()?.setText("")
        clearBackContent()
    }

    /**
     * 清除焦点
     */
    private fun clearFocus() {
        label_scan?.getEditWidget()?.clearFocus()
        label_yuan_liao?.getEditWidget()?.clearFocus()
        label_lingyong_shuliang?.getEditWidget()?.clearFocus()
    }

    /**
     * 清除返回内容
     */
    private fun clearBackContent() {
        label_yuanliao_id?.getContentWidget()?.text = ""
        label_gongdan_id?.getContentWidget()?.text = ""
        label_gongying_shanghao?.getContentWidget()?.text = ""
        label_gongying_shangming?.getContentWidget()?.text = ""
        label_guige_xinhao?.getContentWidget()?.text = ""
        label_kehu_mingcheng?.getContentWidget()?.text = ""
        label_kefang_pinming?.getContentWidget()?.text = ""
        label_chengpin_guige_xinghao?.getContentWidget()?.text = ""
        label_yuan_kucun_shu?.getContentWidget()?.text = ""
        label_lingyong_shuliang?.getEditWidget()?.setText("")
        label_gongdan_zuqiu_shuliang?.getContentWidget()?.text = ""
        label_yilingyong_shuliang?.getContentWidget()?.text = ""
    }

    override fun commitFailed(message: String, exit: Boolean) {
        showToast(message.ifEmpty { (if (exit) getString(R.string.commit_failed) else getString(R.string.code_scanning_failure)) })
    }
}